//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "RiskyFixedBond.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Currency.h>
#include <gen/QL/Termstructures/DefaultProbabilityTermStructure.h>
#include <gen/QL/Times/Schedule.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/CashFlow.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Experimental/Credit/RiskyBond.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Times;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CRiskyFixedBond::CRiskyFixedBond (String^ name, Cephei::QL::ICurrency^ ccy, Double recoveryRate, Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^ defaultTS, Cephei::QL::Times::ISchedule^ schedule, Double rate, Cephei::QL::Times::IDayCounter^ dayCounter, QL::Times::BusinessDayConventionEnum paymentConvention, Cephei::IVector<Double>^ notionals, Cephei::QL::Termstructures::IYieldTermStructure^ yieldTS, Cephei::QL::IPricingEngine^ QL_Pricer) : CRiskyBond(CRiskyFixedBond::typeid)
{
    CCurrency^ _Cccy;
    CDefaultProbabilityTermStructure^ _CdefaultTS;
    CSchedule^ _Cschedule;
    CDayCounter^ _CdayCounter;
    CoVector<Double>^ _Cnotionals;
    CYieldTermStructure^ _CyieldTS;
    try
    {
#ifdef HANDLE
        _phRiskyFixedBond = NULL;
#endif
        std::string _name = (std::string)ValueHelper::Convert (name);
        _Cccy = safe_cast<CCurrency^> (ccy);
        _Cccy->Lock();
        QuantLib::Currency _ccy = static_cast<QuantLib::Currency> (_Cccy->GetReference ()); 
        QuantLib::Real _recoveryRate = (QuantLib::Real)ValueHelper::Convert (recoveryRate);
        _CdefaultTS = safe_cast<CDefaultProbabilityTermStructure^> (defaultTS);
        _CdefaultTS->Lock();
        Handle<QuantLib::DefaultProbabilityTermStructure>& _defaultTS = static_cast<Handle<QuantLib::DefaultProbabilityTermStructure>&> (_CdefaultTS->GetHandle ()); 
        _Cschedule = safe_cast<CSchedule^> (schedule);
        _Cschedule->Lock();
        QuantLib::Schedule _schedule = static_cast<QuantLib::Schedule> (_Cschedule->GetReference ()); 
        QuantLib::Real _rate = (QuantLib::Real)ValueHelper::Convert (rate);
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter _dayCounter = static_cast<QuantLib::DayCounter> (_CdayCounter->GetReference ()); 
        QuantLib::BusinessDayConvention _paymentConvention = (QuantLib::BusinessDayConvention)paymentConvention ;
        CoVector<Double>^ _Cnotionals = safe_cast<CoVector<Double>^> (notionals);
        _Cnotionals->Lock();
        INativeVector<Double>^ _NCInotionals = _Cnotionals->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCnotionals = safe_cast<CDoubleVector^>(_NCInotionals);
        std::vector<QuantLib::Real> _notionals = static_cast<std::vector<QuantLib::Real>> (_NCnotionals->GetReference ());
        _CyieldTS = safe_cast<CYieldTermStructure^> (yieldTS);
        _CyieldTS->Lock();
        Handle<QuantLib::YieldTermStructure>& _yieldTS = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CyieldTS->GetHandle ()); 
        _ppRiskyFixedBond = new boost::shared_ptr<QuantLib::RiskyFixedBond> (new QuantLib::RiskyFixedBond ( _name,  _ccy,  _recoveryRate,  _defaultTS,  _schedule,  _rate,  _dayCounter,  _paymentConvention,  _notionals,  _yieldTS ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppRiskyFixedBond)->setPricingEngine (_QL_Pricer);
        SetRiskyBond (boost::dynamic_pointer_cast<QuantLib::RiskyBond> (*_ppRiskyFixedBond));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cccy != nullptr) _Cccy->Unlock();
        if (_CdefaultTS != nullptr) _CdefaultTS->Unlock();
        if (_Cschedule != nullptr) _Cschedule->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
        if (_Cnotionals != nullptr) _Cnotionals->Unlock();
        if (_CyieldTS != nullptr) _CyieldTS->Unlock();
    }
}
Cephei::QL::Experimental::Credit::CRiskyFixedBond::CRiskyFixedBond (boost::shared_ptr<QuantLib::RiskyFixedBond>& childNative, Object^ owner) : CRiskyBond(CRiskyFixedBond::typeid)
{
#ifdef HANDLE
	_phRiskyFixedBond = NULL;
#endif
	_ppRiskyFixedBond = &childNative;
    _ppRiskyBond = new boost::shared_ptr<QuantLib::RiskyBond> (boost::dynamic_pointer_cast<QuantLib::RiskyBond> (*_ppRiskyFixedBond));
}
Cephei::QL::Experimental::Credit::CRiskyFixedBond::CRiskyFixedBond (QuantLib::RiskyFixedBond& childNative, Object^ owner) : CRiskyBond(CRiskyFixedBond::typeid)
{
#ifdef HANDLE
	_phRiskyFixedBond = NULL;
#endif
	_ppRiskyFixedBond = new boost::shared_ptr<QuantLib::RiskyFixedBond> (&childNative);
    _ppRiskyBond = new boost::shared_ptr<QuantLib::RiskyBond> (boost::dynamic_pointer_cast<QuantLib::RiskyBond> (*_ppRiskyFixedBond));
    _RiskyFixedBondOwner = owner;
    _RiskyBondOwner = owner;
}

Cephei::QL::Experimental::Credit::CRiskyFixedBond::CRiskyFixedBond (CRiskyFixedBond^ copy) : CRiskyBond(CRiskyFixedBond::typeid)
{
#ifdef HANDLE
	_phRiskyFixedBond = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppRiskyFixedBond = new boost::shared_ptr<QuantLib::RiskyFixedBond> (copy->GetShared());
        _ppRiskyBond = new boost::shared_ptr<QuantLib::RiskyBond> (boost::dynamic_pointer_cast<QuantLib::RiskyBond> (*_ppRiskyFixedBond));
    }
}
Cephei::QL::Experimental::Credit::CRiskyFixedBond::CRiskyFixedBond (System::Type^ t) : CRiskyBond(CRiskyFixedBond::typeid)
{
#ifdef HANDLE
	_phRiskyFixedBond = NULL;
#endif
	if (!t->IsSubclassOf(CRiskyFixedBond::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CRiskyFixedBond::CRiskyFixedBond (QuantLib::Handle<QuantLib::RiskyFixedBond>& childNative, Object^ owner)  : CRiskyBond(CRiskyFixedBond::typeid)
{
	_phRiskyFixedBond = &childNative;
	_ppRiskyFixedBond = &static_cast<boost::shared_ptr<QuantLib::RiskyFixedBond>>(childNative.currentLink());
    _ppRiskyBond = new boost::shared_ptr<QuantLib::RiskyBond> (boost::dynamic_pointer_cast<QuantLib::RiskyBond> (*_ppRiskyFixedBond));
    _RiskyFixedBondOwner = owner;
}
Cephei::QL::Experimental::Credit::CRiskyFixedBond::CRiskyFixedBond (QuantLib::Handle<QuantLib::RiskyFixedBond> childNative)  : CRiskyBond(CRiskyFixedBond::typeid)
{
	_phRiskyFixedBond = &childNative;
	_ppRiskyFixedBond = &static_cast<boost::shared_ptr<QuantLib::RiskyFixedBond>>(childNative.currentLink());
    _ppRiskyBond = new boost::shared_ptr<QuantLib::RiskyBond> (boost::dynamic_pointer_cast<QuantLib::RiskyBond> (*_ppRiskyFixedBond));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CRiskyFixedBond::CRiskyFixedBond (QuantLib::RiskyFixedBond childNative)  : CRiskyBond(CRiskyFixedBond::typeid)
{
#ifdef HANDLE
	_phRiskyFixedBond = NULL;
#endif
	_ppRiskyFixedBond = new boost::shared_ptr<QuantLib::RiskyFixedBond> (new QuantLib::RiskyFixedBond (childNative));
    _ppRiskyBond = new boost::shared_ptr<QuantLib::RiskyBond> (boost::dynamic_pointer_cast<QuantLib::RiskyBond> (*_ppRiskyFixedBond));
}
#endif

Cephei::QL::Experimental::Credit::CRiskyFixedBond::~CRiskyFixedBond ()
{
    if (_ppRiskyFixedBond != NULL)
    {
	    delete _ppRiskyFixedBond;
        _ppRiskyFixedBond = NULL;
    }
}
Cephei::QL::Experimental::Credit::CRiskyFixedBond::!CRiskyFixedBond ()
{
    if (_ppRiskyFixedBond != NULL)
    {
	    delete _ppRiskyFixedBond;
    }
}
QuantLib::RiskyFixedBond& Cephei::QL::Experimental::Credit::CRiskyFixedBond::GetReference ()
{
    if (_ppRiskyFixedBond == NULL) throw gcnew NativeNullException ();
	return **_ppRiskyFixedBond;
}
boost::shared_ptr<QuantLib::RiskyFixedBond>& Cephei::QL::Experimental::Credit::CRiskyFixedBond::GetShared ()
{
    if (_ppRiskyFixedBond == NULL) throw gcnew NativeNullException ();
	return *_ppRiskyFixedBond;
}
QuantLib::RiskyFixedBond* Cephei::QL::Experimental::Credit::CRiskyFixedBond::GetPointer ()
{
    if (_ppRiskyFixedBond == NULL) throw gcnew NativeNullException ();
	return &**_ppRiskyFixedBond;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::RiskyFixedBond>& Cephei::QL::Experimental::Credit::CRiskyFixedBond::GetHandle ()
{
	if (_phRiskyFixedBond == NULL)
	{
		_phRiskyFixedBond = new Handle<QuantLib::RiskyFixedBond> (*_ppRiskyFixedBond);
	}
	return *_phRiskyFixedBond;
}
#endif
bool Cephei::QL::Experimental::Credit::CRiskyFixedBond::HasNative () 
{
	return (_ppRiskyFixedBond != NULL);
}

Cephei::IVector<Cephei::QL::ICashFlow^>^ Cephei::QL::Experimental::Credit::CRiskyFixedBond::Cashflows::get ()
{
    try
    {
    	std::vector<boost::shared_ptr<QuantLib::CashFlow> > _rv = (std::vector<boost::shared_ptr<QuantLib::CashFlow> >)(*_ppRiskyFixedBond)->cashflows ( );   
        CoVector<Cephei::QL::ICashFlow^>^ _nrv = gcnew CoVector<Cephei::QL::ICashFlow^>(gcnew CCashFlowVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Experimental::Credit::CRiskyFixedBond::EffectiveDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppRiskyFixedBond)->effectiveDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Cephei::QL::ICashFlow^>^ Cephei::QL::Experimental::Credit::CRiskyFixedBond::InterestFlows::get ()
{
    try
    {
    	std::vector<boost::shared_ptr<QuantLib::CashFlow> > _rv = (std::vector<boost::shared_ptr<QuantLib::CashFlow> >)(*_ppRiskyFixedBond)->interestFlows ( );   
        CoVector<Cephei::QL::ICashFlow^>^ _nrv = gcnew CoVector<Cephei::QL::ICashFlow^>(gcnew CCashFlowVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Experimental::Credit::CRiskyFixedBond::MaturityDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppRiskyFixedBond)->maturityDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CRiskyFixedBond::Notional (Microsoft::FSharp::Core::FSharpOption<DateTime>^ date)
{
    try
    {
        QuantLib::Date _date = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (date) ? (QuantLib::Date)ValueHelper::Convert (date->Value) : Date::minDate()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppRiskyFixedBond)->notional ( _date );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Cephei::QL::ICashFlow^>^ Cephei::QL::Experimental::Credit::CRiskyFixedBond::NotionalFlows::get ()
{
    try
    {
    	std::vector<boost::shared_ptr<QuantLib::CashFlow> > _rv = (std::vector<boost::shared_ptr<QuantLib::CashFlow> >)(*_ppRiskyFixedBond)->notionalFlows ( );   
        CoVector<Cephei::QL::ICashFlow^>^ _nrv = gcnew CoVector<Cephei::QL::ICashFlow^>(gcnew CCashFlowVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Credit::IRiskyFixedBond^ Cephei::QL::Experimental::Credit::CRiskyFixedBond_Factory::Create (String^ name, Cephei::QL::ICurrency^ ccy, Double recoveryRate, Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^ defaultTS, Cephei::QL::Times::ISchedule^ schedule, Double rate, Cephei::QL::Times::IDayCounter^ dayCounter, QL::Times::BusinessDayConventionEnum paymentConvention, Cephei::IVector<Double>^ notionals, Cephei::QL::Termstructures::IYieldTermStructure^ yieldTS, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return gcnew CRiskyFixedBond ( name,  ccy,  recoveryRate,  defaultTS,  schedule,  rate,  dayCounter,  paymentConvention,  notionals,  yieldTS,  QL_Pricer);
}
